
package model.test;

import java.util.Iterator;
import java.util.LinkedList;

/**
 *
 * @author FELIPE & NICOLAS
 */
public class Section {
    
    private String name;
    private int timeLimit;
    private String descTxt;
    private LinkedList<SubSection> subSections;
    
    
    //constructor
    public Section(String name, int timeLimit, String descTxt){
        
        this.name=name;
        this.timeLimit=timeLimit;
        this.descTxt=descTxt;
        subSections = new LinkedList<SubSection>();
        
    }
    
    
    ////constructor 2 (from a string readed from a txt file, which has been pased by the TEST constructor)
    public Section (String inputString){
        
        subSections = new LinkedList<SubSection>();
        
        String[] readResultSection = inputString.split("@"); //readResultSection is an array that represents each Section, which contains info and in each element a subsection
        
        //Data about Section
        this.name = readResultSection[0];       
        this.timeLimit= Integer.parseInt(readResultSection[1]);
        this.descTxt = readResultSection[2];
        
        //create its subSections passing the rest of the elements in the String and add them to this Section
        for (int j=3;j<readResultSection.length;j++){
            
            SubSection subSection = new SubSection(readResultSection[j]);
            subSections.add(subSection);
        }
   
    }
    
    
    
    //method to add a subSection to this Section
    public void addSubSection(SubSection subSection){
        
        subSections.add(subSection);
    }
    
    //method to remove a subSectiom from this Section
    public void removeSubSection(SubSection subSection){
        
        subSections.remove(subSection); 
        
    }
    
    
    
    //GETTERS AND SETTERS

    public String getDescTxt() {
        return descTxt;
    }

    public void setDescTxt(String descTxt) {
        this.descTxt = descTxt;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public LinkedList<SubSection> getSubSections() {
        return subSections;
    }

    public void setSubSections(LinkedList<SubSection> subSections) {
        this.subSections = subSections;
    }

    public int getTimeLimit() {
        return timeLimit;
    }

    public void setTimeLimit(int timeLimit) {
        this.timeLimit = timeLimit;
    }

    //toString method
    public String toString(){
        
        String result=name+"@"+timeLimit+"@"+descTxt;
        
        Iterator<SubSection> iter = subSections.iterator();//create an iterator to navigate through the elements of the collection
        
        while ( iter.hasNext() ){
            result=result+("@")+(iter.next().toString() );
        }
        
        return result;
    }

    //FOR TESTING PURPOSE
    public static void main(String[] args) {
		
	 //create a section
	 Section tempSect= new Section("SectionA",50,"Bla bla bla");
         System.out.println(tempSect);
         
         //create 2 subsections and add them to the section
         SubSection temp = new SubSection("subsection1","hahaha");
         SubSection temp2 = new SubSection("subsection2","hahaha");
         tempSect.addSubSection(temp);
         tempSect.addSubSection(temp2);
         System.out.println(tempSect);

    }
    
}
